package com.xudow.app.newui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.activeshare.edu.ucenter.common.messages.user.UserSecretMessage;
import com.activeshare.edu.ucenter.models.user.UserProfileWithOther;
import com.activeshare.edu.ucenter.models.user.UserSecret;
import com.xudow.app.Config;
import com.xudow.app.R;
import com.xudow.app.newui.base.BaseUiActivity;
import com.xudow.app.newui.zutils.DataCleanManager;
import com.xudow.app.newui.zutils.ZPreferenceUtils;
import com.xudow.app.ui.task.UserSecretTask;
import com.xudow.app.util.ImageUtils;

import java.io.File;
import java.util.HashMap;

import butterknife.BindView;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 作者：zhouXJ on 2016/4/6 14:54\
 * 设置界面
 */

public class SettingActivity extends BaseUiActivity implements View.OnClickListener {

    @BindView(R.id.setting_personal_data_rl)
    RelativeLayout setting_personal_data_rl;

    @BindView(R.id.setting_account_num_rl)
    RelativeLayout setting_account_num_rl;

    @BindView(R.id.setting_privacy_rl)
    RelativeLayout setting_privacy_rl;

    @BindView(R.id.setting_message_push_rl)
    RelativeLayout setting_message_push_rl;

    @BindView(R.id.setting_clear_cache_rl)
    RelativeLayout setting_clear_cache_rl;

    @BindView(R.id.data_size_tv)
    TextView data_size_tv;

    @BindView(R.id.user_img)
    CircleImageView user_img;//用户头像

    UserProfileWithOther profile;
    UserSecret userSecret;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        profile=getMyApp().getProfile();
       // Log.e("onCreate: ", (null == profile) + "" + profile.getPhotoPath());
    }

    @Override
    protected void initData() {
        //子线程计算缓存大小
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                getDataSize();
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        //加载用户头像图片
        if (null!=profile.getPhotoPath()) {
            String url = String.format("%s%s?name=%s", Config.BASE_URL, Config.IMAGE_LOAD_URL, profile.getPhotoPath());
            ImageUtils.loadImage(this, user_img, url, R.mipmap.def_user_ico);
        }
        //获取用户隐私设置信息
        HashMap<String,String> pramer=new HashMap<String, String>();
        pramer.put("userProfileId",profile.getId()+"");
        UserSecretTask getSecretInforTask=new UserSecretTask(this,userSecretInforHandler);
        addTask(getSecretInforTask);
        getSecretInforTask.execute(pramer);
    }

    /**
     * 获取用户隐私信息handler
     */
    private Handler userSecretInforHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what==UserSecretTask.SUCCESS){

                userSecret=(UserSecret)msg.obj;
                if (null!=userSecret)
                //本地保存用户隐私设置
                    ZPreferenceUtils.saveUserSecret(SettingActivity.this,userSecret);
            }else if (msg.what==DELET_CACH){
                hideLoadingDialog();
                getDataSize();
                getMyApp().makeToast("清除成功");
            } else{
                String message=msg.getData().getString("errMessage");
                getMyApp().makeToast(message);
            }
        }
    };
    @Override
    protected void initListener() {
        setting_personal_data_rl.setOnClickListener(this);
        setting_account_num_rl.setOnClickListener(this);
        setting_privacy_rl.setOnClickListener(this);
        setting_message_push_rl.setOnClickListener(this);
        setting_clear_cache_rl.setOnClickListener(this);
    }

    public void onBackBtnClick(View view) {
        finish();
    }

    private final int DELET_CACH=2;
    @Override
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.setting_personal_data_rl: // 个人资料
                intent=new Intent(this, PersonalDataActivity.class);
                startActivity(intent);
                break;

            case R.id.setting_account_num_rl: // 账号管理
               intent=new Intent(this, AccountNumActivity.class);
               startActivity(intent);
                break;

            case R.id.setting_privacy_rl: // 隐私设置
                intent=new Intent(this, PrivacyActivity.class);
               startActivity(intent);
                break;

            case R.id.setting_message_push_rl: // 消息推送
                intent=new Intent(this, MessagePushActivity.class);
                startActivity(intent);
                break;

            case R.id.setting_clear_cache_rl: // 清除缓存
                showLodingDialog("正在清除");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        DataCleanManager.cleanFiles(SettingActivity.this);
                        DataCleanManager.cleanApplicationData(SettingActivity.this, new String[]{file1.getAbsolutePath(),
                                file2.getAbsolutePath(), file3.getAbsolutePath()});
                        userSecretInforHandler.sendEmptyMessage(DELET_CACH);

                    }
                });
                break;
        }
    }

    /**
     * 获取缓存数据大小
     */
    File file1,file2,file3;
    private void getDataSize() {
          file1=new File(getCacheDir().getAbsoluteFile()+"/picasso-cache/");
          file2=new File(getExternalCacheDir().getAbsolutePath()+"/picasso-cache/");
          file3=new File(Config.SDCARD_ROOT_DIR + "/update/");//更新下载目录
        long total=0;
        String size="";
        try {
            if (file3.exists()||file1.exists()||file2.exists()) {
                total = DataCleanManager.getFolderSize(file1) +
                        DataCleanManager.getFolderSize(file2)+DataCleanManager.getFolderSize(file3);
                size = DataCleanManager.getFormatSize(total);
            }
            data_size_tv.setText(size);
            data_size_tv.setVisibility(View.VISIBLE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
